package com.linran.webjsonrsp.controller;

import com.linran.webjsonrsp.annotation.ResponseResultBody;
import com.linran.webjsonrsp.bean.Result;
import com.linran.webjsonrsp.bean.exception.ResultException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * RestControllerAdvice返回测试
 *
 * @author lin
 * @date 2020-05-05 17:04
 */
@RestController
@ResponseResultBody//自定义注解(标注该类需要进行统一json处理，或者如下继承JsonController进行统一json处理拦截)
@RequestMapping("/helloResult")
public class HelloResultController extends JsonController {
    private static final HashMap<String, Object> INFO;

    static {
        INFO = new HashMap<>();
        INFO.put("name", "galaxy");
        INFO.put("age", "70");
        INFO.put("localDateTime", LocalDateTime.now());
    }

    @GetMapping("hello")
    public HashMap<String, Object> hello() {
        return INFO;
    }

    /**
     * 测试重复包裹
     */
    @GetMapping("result")
    public Result<Map<String, Object>> helloResult() {
        return Result.success(INFO);
    }

    /**
     * 测试普通异常
     */
    @GetMapping("helloError")
    public HashMap<String, Object> helloError() throws Exception {
        throw new Exception("异常信息不会显示");
    }

    /**
     * 测试自定义异常
     */
    @GetMapping("helloMyError")
    public HashMap<String, Object> helloMyError() throws ResultException {
        throw new ResultException("自定义异常信息会显示");
    }
}
